package com.zy.ipanalysis.controller;

import com.zy.ipanalysis.model.Isp;
import com.zy.ipanalysis.service.IspService;
import com.zy.commom.utils.MailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * Created by ZYDR on 2015/12/27.
 */
@Controller
@RequestMapping("/isp")
public class IspController {
    @Autowired
    IspService ispService;
    @RequestMapping("/import")
    public String writeIsp2DataBase(Model model){
        String[] isps={"中国电信","中国移动","中国联通","中国铁通","中国教育网","中国科技网"};
        try {
            if (ispService.count()>1){
                model.addAttribute("result","已有数据，不用重复添加");
                return "index";
            }
            int i=0;
            while (i<isps.length){
                Isp isp=new Isp();
                isp.setIspName(isps[i]);
                ispService.insert(isp);
                i++;
            }
            MailUtil.sendMail(MailUtil.DEFAULT_TARGET,"数据操作成功","操作代号：02<br>向t_isp表中插入了"+i+"条数据<br>");
            model.addAttribute("result","插入"+i+"条数据");
        }
        catch (Exception e){
            model.addAttribute("result",e.getMessage());
            MailUtil.sendMail(MailUtil.DEFAULT_TARGET,MailUtil.FAILURE_TITLE,"操作代号：01<br>"+e.getMessage());
        }
        return  "index";
    }
}
